//
//  cmdUtilities.h
//  autocompilerCMD
//
//  Created by Vescky on 13-5-17.
//  Copyright (c) 2013年 Vescky. All rights reserved.
//

#ifndef autocompilerCMD_cmdUtilities_h
#define autocompilerCMD_cmdUtilities_h

CG_INLINE BOOL isValue(NSString* str);
CG_INLINE BOOL hasInvalidArgs(int argsCount, const char * args[]);

CG_INLINE BOOL isArgsValid(int argsCount, const char * args[]) {
    if (hasInvalidArgs(argsCount, args)) {
        printf("Type 'automaker help' for details!\n\n");
        exit(0);
    }
    
    @autoreleasepool {
        if (argsCount == 1) {
            printf("\n\nYour should pass args when you call autoMaker! type 'autoMaker -help' for details!\n\n");
        }
        else if (argsCount == 2) {
            NSString *str = [NSString stringWithUTF8String:args[1]];
            str = [str lowercaseString];
            if ([str isEqualToString:@"help"] || [str isEqualToString:@"-help"] || [str isEqualToString:@"-h"] || [str isEqualToString:@"h"]) {
                printf("\n\n---------------------------------USAGE----------------------------------------\n");
                printf("Tips:此命令支持中英文路径，参数不分先后次序,参数名(如'-ios')不分大小写.\n");
                printf("\n调用方法:'./automaker -enname enName [--build] [--save] [-out outPath] [-ios iosPath] [-android androidPath] [-environment wip/prod] [-zipurl zipurl] '\n\n");
                printf("带有[]的参数是可选参数，但是'enname'这个参数是必须有的。\n");
                printf("-enname: 项目的英文名，将以此名字命名项目。\n");
                printf("--build: 可选，若加上此参数，则生成项目后会自动编译项目。 \n");
                printf("--save: 可选，若加上此参数，编译项目时编译信息储存到日志文件。 \n");
                printf("-out: 可选，指定工作目录.不影响原来的工作目录.若指定的目录已存在，则会在目录名称后面加上编号。\n");
                printf("-ios: 可选，ios模板的目录。加了'-ios ',但是后面没加路径，则用默认路径。指定目录需要指定到xx/xx/AppsTemplate \n");
                printf("-android: 可选，android模板的目录。同上。 \n");
                printf("-mytemplate: 可选，指定defualtTemplate的目录。此参数会覆盖'-ios'和'-android'所设置的路径 \n");
                printf("-environment: 可选，指定服务器环境，'wip'或者'prod',默认是prod. \n");
                printf("-zipurl: 可选，指定zip包的下载地址.\n\t1)'file:'开头表示本地文件 \n\t2)'http:\\\\'开头表示网络地址，此地址是浏览器可以打开并下载的，不是svn地址,无网络头则默认为本地地址 \n\n");
//                printf("\n注意: 若要生成不同的工程，用于测试或者抽取项目，请输入参数-zipurl，或者手动到工作目录中更换'tmp'目录中的'projectinfo.bundle' 为zip包解压后的文件夹，并将文件夹重命名为'projectinfo.bundle' .\n\n");
                
                printf("------------------------------------------------------ coded by Vescky %s -----\n\n",[LAST_UPDATE_DATE UTF8String]);
            }
            else if ([str isEqualToString:@"version"] || [str isEqualToString:@"-version"] || [str isEqualToString:@"v"] || [str isEqualToString:@"-v"] ) {
                printf("\nCurrent Version: %s\n\n",[CURRENT_VERSION UTF8String]);
            }
            else if ([str isEqualToString:@"-ios"] || [str isEqualToString:@"-enname"] || [str isEqualToString:@"-android"] || [str isEqualToString:@"-out"] || [str isEqualToString:@"-zipurl"] || [str isEqualToString:@"-environment"]|| [str isEqualToString:@"-mytemplate"] || [str isEqualToString:@"--build"] || [str isEqualToString:@"--save"] ) {
                printf("\n\nToo less arg passed! type 'automaker -help' for details!\n\n");
            }
            else {
                printf("\n\nInvalid args passed! type 'automaker -help' for details!\n\n");
            }
        }
        else {
            return YES;
        }
    }
    
    return NO;
}


CG_INLINE NSMutableDictionary *getArgsDict(int argsCount, const char * args[]) {
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    
    for (int i = 0; i < argsCount; i++) {
        printf("%s\n",args[i]);
    }
    
    for (int i = 0; i < argsCount; i++) {
        NSString* argStr = [NSString stringWithUTF8String:args[i]];
        argStr = [argStr lowercaseString];
        
        if ([argStr isEqualToString:@"-out"]) {
            if (i+1 < argsCount) {
                NSString *v = [NSString stringWithUTF8String:args[i+1]];
                if (isValue(v)) {
                    [dict setObject:v forKey:@"out"];
                    i++;
                }
            }
        }
        else if  ([argStr isEqualToString:@"-ios"]) {
            if (i+1 < argsCount) {
                NSString *v = [NSString stringWithUTF8String:args[i+1]];
                if (isValue(v)) {
                    [dict setObject:v forKey:@"ios"];
                    i++;
                }
                else {
                    [dict setObject:@"default" forKey:@"ios"];
                }
            }
            else {
                [dict setObject:@"default" forKey:@"ios"];
            }
        }
        else if  ([argStr isEqualToString:@"-android"]) {
            if (i+1 < argsCount) {
                NSString *v = [NSString stringWithUTF8String:args[i+1]];
                if (isValue(v)) {
                    [dict setObject:v forKey:@"android"];
                    i++;
                }
                else {
                    [dict setObject:@"default" forKey:@"android"];
                }
            }
            else {
                [dict setObject:@"default" forKey:@"android"];
            }
        }
        else if  ([argStr isEqualToString:@"-enname"]) {
            if (i+1 < argsCount) {
                NSString *v = [NSString stringWithUTF8String:args[i+1]];
                if (isValue(v)) {
                    [dict setObject:v forKey:@"enname"];
                    i++;
                }
            }
        }
        else if  ([argStr isEqualToString:@"-environment"]) {
            if (i+1 < argsCount) {
                NSString *v = [NSString stringWithUTF8String:args[i+1]];
                if (isValue(v)) {
                    [dict setObject:v forKey:@"environment"];
                    i++;
                }
            }
        }
        else if  ([argStr isEqualToString:@"-zipurl"]) {
            if (i+1 < argsCount) {
//                NSString *v = [NSString stringWithFormat:@"%s",args[i+1]];
                NSString *v = [NSString stringWithUTF8String:args[i+1]];
                if (isValue(v)) {
                    [dict setObject:v forKey:@"zipurl"];
                    i++;
                }
            }
        }
        else if  ([argStr isEqualToString:@"-mytemplate"]) {
            if (i+1 < argsCount) {
                NSString *v = [NSString stringWithUTF8String:args[i+1]];
                if (isValue(v)) {
                    [dict setObject:v forKey:@"mytemplate"];
                    i++;
                }
            }
        }
        else if  ([argStr isEqualToString:@"-profile"]) {
            if (i+1 < argsCount) {
                NSString *v = [NSString stringWithUTF8String:args[i+1]];
                if (isValue(v)) {
                    [dict setObject:v forKey:@"profile"];
                    i++;
                }
            }
        }
        else if  ([argStr isEqualToString:@"-mode"]) {
            if (i+1 < argsCount) {
                NSString *v = [NSString stringWithUTF8String:args[i+1]];
                if (isValue(v)) {
                    [dict setObject:v forKey:@"mode"];
                    i++;
                }
            }
        }
        else if ([argStr isEqualToString:@"--build"]) {
            [dict setObject:@"YES" forKey:@"build"];
        }
        else if ([argStr isEqualToString:@"--save"]) {
            [dict setObject:@"YES" forKey:@"savelogs"];
        }
    }
    
    return dict;
}


CG_INLINE BOOL isValue(NSString* str) {
    if ([str isEqualToString:@"help"] || str.length < 1) {
        return NO;
    }
    NSString *fString = [str substringToIndex:1];
    if ([fString isEqualToString:@"-"]) {
        return NO;
    }
    return YES;
}

CG_INLINE BOOL hasInvalidArgs(int argsCount, const char * args[]) {
    @autoreleasepool {
        NSArray *arr = [[NSArray alloc] initWithObjects: @"help" ,@"-help" ,@"h" ,@"-h" ,@"version" ,@"-version" ,@"v" ,@"-v" ,@"-mode",
                        @"-out",@"-ios",@"-android",@"-enname",@"-zipurl",@"-environment",@"-mytemplate",@"-profile",@"--build",@"--save", nil];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        for (int i = 0; i < [arr count]; i++) {
            [dict setObject:@" " forKey:[arr objectAtIndex:i]];
        }
//        NSLog(@"%@",dict);
        
        for (int i = 1; i < argsCount; i++) {
            NSString *str = [NSString stringWithUTF8String:args[i]];
            str = [str lowercaseString];
//            NSLog(@"%@",str);
            if (!isValue(str)) {
                //参数
                NSString *sss = [dict objectForKey:str];
                if (!sss) {
                    //为空，找不到该参数
                    printf("\n '%s' is an invalid argument!\n",[str UTF8String]);
                    return YES;
                }
            }
        }
        return NO;
    }
    
}

#endif
